home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Miscellaneous / Randy Thelen / ThreadedSort / Lib / Window.cp < prev   
Encoding:
Text File  |  1994-06-26  |  18.2 KB  |  749 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Window.cp
  3.  
  4.     Contains:    Implementation of TWindow, a base class which provides a
  5.                 framework for building way-cool windows which even John
  6.                 Sullivan would be happy with. Floating windows and “smart
  7.                 zooming” algorithms are based on code samples provided by
  8.                 Dean Yu.
  9.                 
  10.     Written by: Dave Falkenburg
  11.  
  12.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  13.  
  14.     Change History (most recent first):
  15.     
  16.     To Do:        Create invisible, and bug free showing and hiding windows
  17.                 Window positioning methods (getters and setters)
  18.                 Display Manager support: AdjustWindowForNewScreen?
  19.                 Changes to support AEObject model
  20.  */
  21.  
  22. #include <Types.h>
  23. #include <Windows.h>
  24. #include <Errors.h>
  25. #include <Script.h>        //    for GetMBarHeight()
  26. #include <LowMem.h>        //    for LMGetWindowList()
  27.  
  28. #include "AppLib.h"
  29. #include "Window.h"
  30.  
  31. const short            kFloatingWindowKind        = 1000;
  32. const short            kNormalWindowKind        = 1001;
  33. const WindowPtr     kNoFloatingWindows        = (WindowPtr) -1;
  34.  
  35. const short            kScreenEdgeSlop            = 4;
  36. const short            kSpaceForFinderIcons    = 64;
  37. const short            kMinimumTitleBarHeight    = 21;
  38. const short            kMinimumWindowSize        = 32;
  39.  
  40. static void            HiliteShowHideFloatingWindows(Boolean hiliting,Boolean hiding);
  41.  
  42. static void            FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect, GDHandle * theBestDevice);
  43. static pascal void    CalculateWindowAreaOnDevice(short depth,short deviceFlags,GDHandle targetDevice,long userData);
  44.  
  45. struct    CalcWindowAreaDeviceLoopUserData
  46.     {
  47.     GDHandle    fScreenWithLargestPartOfWindow;
  48.     long        fLargestArea;
  49.     Rect        fWindowBounds;
  50.     };
  51.  
  52.  
  53. TWindow::TWindow()
  54.     {
  55.     }
  56.  
  57. TWindow::~TWindow()
  58.     {
  59.     }
  60.  
  61. void
  62. TWindow::CreateWindow(Boolean isFloating)
  63.     {
  64.     WindowPtr    behindWindow,oldFrontMostWindow;
  65.     
  66.     if (isFloating)
  67.         {
  68.         behindWindow = (WindowPtr) -1;
  69.         oldFrontMostWindow = FrontWindow();
  70.         }
  71.     else
  72.         {
  73.         behindWindow = LastFloatingWindow();
  74.         
  75.         if (behindWindow == kNoFloatingWindows)
  76.             oldFrontMostWindow = nil;
  77.         else
  78.             oldFrontMostWindow = (WindowPtr) ((WindowPeek) behindWindow)->nextWindow;
  79.         }
  80.             
  81.     fWindow = MakeNewWindow(behindWindow);
  82.  
  83.     if (fWindow)
  84.         {
  85.         SetWRefCon(fWindow,(long) this);
  86.  
  87.         fIsFloatingWindow = isFloating;
  88.         fIsVisible = true;                    //    should be visibleFlag
  89.         
  90.         ((WindowPeek) fWindow)->windowKind = kNormalWindowKind;
  91.  
  92.         if (isFloating)
  93.             {
  94.             ((WindowPeek) fWindow)->windowKind = kFloatingWindowKind;
  95.             
  96.             //    make sure the other window stays hilited
  97.  
  98.             if (oldFrontMostWindow)
  99.                 HiliteAndActivateWindow(oldFrontMostWindow,true);
  100.             }
  101.         else
  102.             {
  103.             ((WindowPeek) fWindow)->windowKind = kNormalWindowKind;
  104.  
  105.             //    unhighlight the old front window
  106.  
  107.             if (oldFrontMostWindow)
  108.                 HiliteAndActivateWindow(oldFrontMostWindow,false);
  109.  
  110.             //    hilite the new window…
  111.             
  112.             HiliteAndActivateWindow(fWindow,true);
  113.             }
  114.         }
  115.     }
  116.  
  117.  
  118. void
  119. TWindow::AdjustCursor(EventRecord * /* anEvent */)
  120.     {
  121.     }
  122.  
  123. void
  124. TWindow::Idle(EventRecord * /* anEvent */)
  125.     {
  126.     }
  127.     
  128. void
  129. TWindow::Activate(Boolean /* activating */)
  130.     {
  131.     }
  132.     
  133. void
  134. TWindow::Draw(void)
  135.     {
  136.     }
  137.     
  138. void
  139. TWindow::Click(EventRecord * /* anEvent */)
  140.     {
  141.     }
  142.     
  143. void
  144. TWindow::KeyDown(EventRecord * /* anEvent */)
  145.     {
  146.     }
  147.  
  148.  
  149. void
  150. TWindow::Select(void)
  151.     {
  152.     WindowPtr    currentFrontWindow;
  153.     
  154.     if (fIsFloatingWindow)
  155.         currentFrontWindow = FrontWindow();
  156.     else
  157.         currentFrontWindow = FrontNonFloatingWindow();
  158.  
  159.     if (currentFrontWindow != fWindow)
  160.         {
  161.         if (fIsFloatingWindow)
  162.             BringToFront(fWindow);
  163.         else
  164.             {
  165.             WindowPtr    lastFloater = LastFloatingWindow();
  166.  
  167.             //    If there are no floating windows,
  168.             //    just call SelectWindow like the good ol’ days
  169.  
  170.             if (lastFloater == kNoFloatingWindows)
  171.                 SelectWindow(fWindow);
  172.             else
  173.                 {
  174.                 // Deactivate the window currently in front.
  175.  
  176.                 HiliteAndActivateWindow(currentFrontWindow,false);
  177.     
  178.                 // Bring it behind the last floating window and activate it.
  179.                 // Note that Inside Mac 1 states that you need to call PaintOne() and CalcVis() on a
  180.                 // window if you are using SendBehind() to bring it closer to the front.  With System 7,
  181.                 // this is no longer necessary.
  182.  
  183.                 SendBehind(fWindow,lastFloater);
  184.                 HiliteAndActivateWindow(fWindow,true);
  185.                 }
  186.             }
  187.         }
  188.     }
  189.  
  190.  
  191. void
  192. TWindow::Drag(Point startPoint)
  193.     {
  194.     GrafPtr        savePort;
  195.     KeyMap        theKeyMap;
  196.     Boolean        commandKeyDown = false;
  197.     RgnHandle    draggingRegion;
  198.     long        dragResult;
  199.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  200.     
  201.     if (WaitMouseUp())        //    de-bounce?
  202.         {
  203.         // Set up the Window Manager port.
  204.     
  205.         GetPort(&savePort);
  206.         SetPort(gWindowManagerPort);
  207.         SetClip(GetGrayRgn());
  208.  
  209.         // Check to see if the command key is down.
  210.     
  211.         GetKeys(theKeyMap);
  212.         commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
  213.         
  214.         if (commandKeyDown)
  215.             {
  216.             //    We’re not going to change window ordering,
  217.             //    so make sure that we don’t drag in front of
  218.             //    other windows which may be in front of ours.
  219.  
  220.             ClipAbove(windowAsWindowPeek);
  221.             }
  222.         else if (!fIsFloatingWindow)
  223.             {
  224.             //    We’re dragging a normal window, so make sure
  225.             //    that we don’t drag in front of any floating
  226.             //    windows.
  227.  
  228.             ClipAbove((WindowPeek) FrontNonFloatingWindow());
  229.             }
  230.         
  231.         //    Drag an outline of the window around the desktop.
  232.         //    NOTE: DragGrayRgn destroys the region passed in, so make a copy
  233.  
  234.         draggingRegion = NewRgn();
  235.         CopyRgn(windowAsWindowPeek->strucRgn,draggingRegion);
  236.         dragResult = DragGrayRgn(draggingRegion, startPoint, &gDeskRectangle, &gDeskRectangle, noConstraint, nil);
  237.         DisposeRgn(draggingRegion);
  238.  
  239.     
  240.         SetPort(savePort);    //    Get back to old port
  241.  
  242.         if ((dragResult != 0) && (dragResult != 0x80008000))
  243.             {
  244.             short         newHorizontalPosition,newVerticalPosition;
  245.  
  246.             newHorizontalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.left + (dragResult & 0xFFFF);
  247.             newVerticalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.top + (dragResult >> 16);
  248.             
  249.             MoveWindow(fWindow,newHorizontalPosition,newVerticalPosition,false);
  250.             }
  251.         }
  252.  
  253.     if (!commandKeyDown)
  254.         Select();
  255.     }
  256.  
  257.  
  258. void
  259. TWindow::Grow(Point startPoint)
  260.     {
  261.     GrafPtr    oldPort;
  262.     long    newSize;
  263.     Rect    oldWindowRect,resizeLimits;
  264.     
  265.     GetPort(&oldPort);
  266.     
  267.     GetWindowSizeLimits(&resizeLimits);
  268.     newSize = GrowWindow(fWindow,startPoint,&resizeLimits);
  269.     if (newSize)
  270.         {
  271.         oldWindowRect = fWindow->portRect;
  272.         SizeWindow(fWindow,(short) newSize,(short) (newSize >> 16),true);
  273.         SetPort(fWindow);
  274.         AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  275.         }
  276.     
  277.     SetPort(oldPort);
  278.     }
  279.  
  280.  
  281. void
  282. TWindow::Zoom(short zoomState)
  283.     {
  284.     GrafPtr        oldPort;
  285.     FontInfo    systemFontInfo;
  286.     short        titleBarHeight;
  287.     Rect        bestScreenRect,perfectWindowRect,scratchRect;
  288.     short        amountOffscreen;
  289.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  290.     GDHandle    bestDevice;
  291.     
  292.     GetPort(&oldPort);
  293.  
  294.     //    Figure out the height of the title bar so we can properly position
  295.     //    a window. The algorithm is stolen from the System 7.x 'WDEF' (0)
  296.     //
  297.     //    This probably isn’t the best thing to do: A better way might be 
  298.     //    to diff the structure and content region rectangles?
  299.  
  300.     SetPort(gWindowManagerPort);
  301.     GetFontInfo(&systemFontInfo);
  302.     titleBarHeight = (short) (systemFontInfo.ascent + systemFontInfo.descent + 4);
  303.     if ((titleBarHeight % 2) == 1)
  304.         titleBarHeight--;
  305.     if (titleBarHeight < kMinimumTitleBarHeight)
  306.         titleBarHeight = kMinimumTitleBarHeight;
  307.  
  308.  
  309.     //    Only do the voodoo magic if we are really “zooming” the window.
  310.  
  311.     if (zoomState == inZoomOut)
  312.         {
  313.         FindScreenRectWithLargestPartOfWindow(fWindow,&bestScreenRect,&bestDevice);
  314.         bestScreenRect.top += titleBarHeight;
  315.  
  316.         GetPerfectWindowSize(&perfectWindowRect);
  317.         OffsetRect(&perfectWindowRect,-perfectWindowRect.left,-perfectWindowRect.top);
  318.  
  319.         //    Take the zero-pined perfect window size and move it to
  320.         //    the top left of the    window’s content region.
  321.  
  322.         OffsetRect(&perfectWindowRect,(**windowAsWindowPeek->contRgn).rgnBBox.left,
  323.                                       (**windowAsWindowPeek->contRgn).rgnBBox.top);
  324.  
  325.         
  326.         //    Does perfectWindowRect fit completely on the best screen?
  327.         
  328.         SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  329.         if (!EqualRect(&perfectWindowRect, &scratchRect))
  330.             {
  331.             //    SectRect sez perfectWindowRect doesn’t completely fit
  332.             //    on the screen, so bump the window so that more of it fits.
  333.  
  334.             //    Make sure that the left edge of perfectWindowRect is forced
  335.             //    onto the best screen.  This is in case we are bumping
  336.             //    the window to the right.
  337.  
  338.             amountOffscreen = bestScreenRect.left - perfectWindowRect.left;
  339.             if (amountOffscreen > 0)
  340.                 {
  341.                 perfectWindowRect.left += amountOffscreen;
  342.                 perfectWindowRect.right += amountOffscreen;
  343.                 }
  344.  
  345.             //    Make sure that the left edge of perfectWindowRect is forced
  346.             //    onto the best screen.  This is in case we are bumping
  347.             //    the window downward to a new screen.
  348.     
  349.             amountOffscreen = bestScreenRect.top - perfectWindowRect.top;
  350.             if (amountOffscreen > 0)
  351.                 {
  352.                 perfectWindowRect.top += amountOffscreen;
  353.                 perfectWindowRect.bottom += amountOffscreen;
  354.                 }
  355.  
  356.             //    If right edge of window falls off the screen,
  357.             //        Move window to the left until the right edge IS on the screen
  358.             //        OR the left edge is at bestScreenRect.left
  359.  
  360.             amountOffscreen = perfectWindowRect.right - bestScreenRect.right;
  361.             if (amountOffscreen > 0)
  362.                 {
  363.                 //    Are we going to push the left edge offscreen? If so, change the
  364.                 //    offset so we move the window all the way over to the left.
  365.                 
  366.                 if ((perfectWindowRect.left - amountOffscreen) < bestScreenRect.left)
  367.                     amountOffscreen = perfectWindowRect.left - bestScreenRect.left;
  368.  
  369.                 perfectWindowRect.left -= amountOffscreen;
  370.                 perfectWindowRect.right -= amountOffscreen;
  371.                 }
  372.  
  373.             //    If bottom edge of window falls off the screen,
  374.             //        Move window to up until the bottom edge IS on the screen
  375.             //        OR the top edge is at bestScreenRect.top
  376.  
  377.             amountOffscreen = perfectWindowRect.bottom - bestScreenRect.bottom;
  378.             if (amountOffscreen > 0)
  379.                 {
  380.                 //    Are we going to push the top edge offscreen? If so, change the
  381.                 //    offset so we move the window just to the top.
  382.                 
  383.                 if ((perfectWindowRect.top - amountOffscreen) < bestScreenRect.top)
  384.                     amountOffscreen = perfectWindowRect.top - bestScreenRect.top;
  385.  
  386.                 perfectWindowRect.top -= amountOffscreen;
  387.                 perfectWindowRect.bottom -= amountOffscreen;
  388.                 }
  389.  
  390.             SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  391.             if (!EqualRect(&perfectWindowRect, &scratchRect))
  392.                 {
  393.                 //    The edges of the window still fall offscreen,
  394.                 //    so make the window smaller until it fits.
  395.                 
  396.                 if (perfectWindowRect.bottom > bestScreenRect.bottom)
  397.                     perfectWindowRect.bottom = bestScreenRect.bottom;
  398.  
  399.                 //    If the right edge is still falling off,
  400.                 //        save space for Finder’s disk icons as well.
  401.  
  402.                 if (perfectWindowRect.right > bestScreenRect.right)
  403.                     {
  404.                     perfectWindowRect.right = bestScreenRect.right;
  405.                     
  406.                     //    If we were on the main screen, leave room for Finder icons, too.
  407.                     
  408.                     if (bestDevice == GetMainDevice())
  409.                         perfectWindowRect.right -= kSpaceForFinderIcons;
  410.                     }
  411.                 }
  412.             }
  413.  
  414.         //    Stash our new rectangle inside of the Window’s dataHandle
  415.         //    so that ZoomWindow does the right thing.
  416.         
  417.         (**((WStateDataHandle) (windowAsWindowPeek->dataHandle))).stdState = perfectWindowRect;
  418.         }
  419.  
  420.     //    HEY YOU! Don’t forget to set the port to the window being zoomed
  421.     //    Why, you ask? Because IM-IV-50 says to; otherwise you die
  422.     
  423.     SetPort(fWindow);
  424.  
  425.     Rect    oldWindowRect = fWindow->portRect;
  426.     
  427.     ZoomWindow(fWindow,zoomState,false);
  428.     AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  429.  
  430.     SetPort(oldPort);
  431.     }
  432.  
  433.  
  434. void
  435. TWindow::ShowOrHide(Boolean showFlag)
  436.     {
  437.     //    This wrapper for ShowHide allows us to correctly
  438.     //    show or hide floating windows behind the back of the
  439.     //    rest of the application.
  440.     
  441.     fIsVisible = showFlag;
  442.     ShowHide(fWindow,showFlag);
  443.     }
  444.     
  445.  
  446. Boolean
  447. TWindow::EventFilter(EventRecord * /* theEvent */)
  448.     {
  449.     return (false);
  450.     }
  451.     
  452.  
  453. void
  454. TWindow::GetPerfectWindowSize(Rect *perfectSize)
  455.     {
  456.     *perfectSize = qd.screenBits.bounds;
  457.     }
  458.  
  459. void
  460. TWindow::GetWindowSizeLimits(Rect *limits)
  461.     {
  462.     limits->top = limits->left = kMinimumWindowSize;
  463.     limits->right = gDeskRectangle.right - gDeskRectangle.left;
  464.     limits->bottom = gDeskRectangle.bottom - gDeskRectangle.top;
  465.     }
  466.  
  467. void
  468. TWindow::AdjustForNewWindowSize(Rect * /* oldRect */, Rect * /* newSize */)
  469.     {
  470.     }
  471.  
  472.  
  473. Boolean
  474. TWindow::IsVisible(void)
  475.     {
  476.     return fIsVisible;
  477.     }
  478.  
  479.  
  480. Boolean
  481. TWindow::CanClose(void)
  482.     {
  483.     return(true);
  484.     }
  485.  
  486. Boolean
  487. TWindow::Close(void)
  488.     {
  489.     WindowPtr    newFrontWindow = nil;
  490.     
  491.     if (FrontNonFloatingWindow() == fWindow)
  492.         newFrontWindow = (WindowPtr) ((WindowPeek) fWindow)->nextWindow;
  493.  
  494.     DisposeWindow(fWindow);
  495.  
  496.     if (newFrontWindow)
  497.         HiliteAndActivateWindow(newFrontWindow,true);
  498.     return(true);
  499.     }
  500.  
  501.  
  502. Boolean
  503. TWindow::DeleteAfterClose(void)
  504.     {
  505.     return(true);
  506.     }
  507.  
  508. Boolean
  509. TWindow::CanEdit(void)
  510.     {
  511.     return(false);
  512.     }
  513.  
  514. void
  515. TWindow::DoEditMenu(short /* menuCode */)
  516.     {
  517.     }
  518.  
  519.  
  520. OSErr
  521. TWindow::HandleDrag(DragTrackingMessage /* dragMessage */, DragReference /* theDrag */)
  522.     {
  523.     return(noErr);
  524.     }
  525.  
  526.     
  527. OSErr
  528. TWindow::HandleDrop(DragReference /* theDrag */)
  529.     {
  530.     return(noErr);
  531.     }
  532.  
  533.  
  534. ///////////////////////////////////////////////////////////////////////////
  535. //
  536. //    Utility Functions used for floating windows
  537. //
  538.  
  539. TWindow *
  540. GetWindowObject(WindowPtr aWindow)
  541.     {
  542.     short    wKind;
  543.     
  544.     if (aWindow != nil)
  545.         {
  546.         wKind = ((WindowPeek) aWindow)->windowKind;
  547.  
  548.         if (wKind >= userKind)
  549.             {
  550.             //    All windowKinds >= userKind are based upon TWindow
  551.  
  552.             return (TWindow *) GetWRefCon(aWindow);
  553.             }
  554.         }
  555.     return (TWindow *) nil;
  556.     }
  557.  
  558.  
  559. WindowPtr
  560. LastFloatingWindow(void)
  561.     {
  562.     WindowPeek    aWindow = (WindowPeek) FrontWindow();
  563.     WindowPtr    lastFloater = (WindowPtr) kNoFloatingWindows;
  564.     
  565.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  566.         {
  567.         if (aWindow->visible)
  568.             lastFloater = (WindowPtr) aWindow;
  569.  
  570.         aWindow = (WindowPeek) aWindow->nextWindow;
  571.         }
  572.     return(lastFloater);
  573.     }
  574.  
  575.  
  576. WindowPtr
  577. FrontNonFloatingWindow(void)
  578.     {
  579.     WindowPeek    aWindow = (WindowPeek) LMGetWindowList();
  580.  
  581.     //    Skip over floating windows
  582.         
  583.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  584.         aWindow = (WindowPeek) aWindow->nextWindow;
  585.  
  586.     //    Skip over invisible, but otherwise normal windows
  587.     
  588.     while (aWindow && (aWindow->visible == 0))
  589.         aWindow = (WindowPeek) aWindow->nextWindow;
  590.         
  591.     return (WindowPtr) aWindow;
  592.     }
  593.  
  594.  
  595. void
  596. HiliteAndActivateWindow(WindowPtr aWindow,Boolean active)
  597.     {
  598.     GrafPtr        oldPort;
  599.     TWindow    *    wobj = GetWindowObject(aWindow);
  600.     
  601.     if (aWindow)
  602.         {
  603.         HiliteWindow(aWindow,active);
  604.  
  605.         if (wobj != nil)
  606.             {
  607.             GetPort(&oldPort);
  608.             SetPort(aWindow);
  609.             wobj->Activate(active);
  610.             SetPort(oldPort);
  611.             }    
  612.         }
  613.     }
  614.  
  615. void
  616. SuspendResumeWindows(Boolean resuming)
  617.     {
  618.     //    When we suspend/resume, hide/show all the visible floaters
  619.     
  620.     HiliteShowHideFloatingWindows(resuming,true);
  621.     }
  622.  
  623. void
  624. HiliteWindowsForModalDialog(Boolean hiliting)
  625.     {
  626.     //    When we display a modal dialog, we need to unhighlight
  627.     //    all visible floaters. We also need to re-hilite them
  628.     //    afterwards.
  629.     
  630.     HiliteShowHideFloatingWindows(hiliting,false);
  631.     }
  632.  
  633. void
  634. HiliteShowHideFloatingWindows(Boolean hiliting,Boolean dohiding)
  635.     {
  636.     WindowPeek    aWindow;
  637.     TWindow *    wobj;
  638.     
  639.     HiliteAndActivateWindow(FrontNonFloatingWindow(),hiliting);
  640.  
  641.     aWindow = LMGetWindowList();
  642.     while (aWindow && aWindow->windowKind == kFloatingWindowKind)
  643.         {
  644.         wobj = GetWindowObject((WindowPtr) aWindow);
  645.         
  646.         //    If we are hiding or showing, only hide/show windows
  647.         //    that were visible to begin with.
  648.          
  649.         if (dohiding && (wobj != nil) && ((wobj->IsVisible()) || (!hiliting)))
  650.             ShowHide((WindowPtr) aWindow,hiliting);
  651.  
  652.         //    All floaters are hilited if any floater is hilited
  653.  
  654.         HiliteWindow((WindowPtr) aWindow,hiliting);
  655.         aWindow = (WindowPeek) aWindow->nextWindow;
  656.         }
  657.     }
  658.  
  659.  
  660. ///////////////////////////////////////////////////////////////////////////
  661. //
  662. //    Routines used for dealing with windows and multiple screens
  663. //
  664.  
  665. pascal void
  666. CalculateWindowAreaOnDevice(short /* depth */,short /* deviceFlags */,GDHandle targetDevice,long userData)
  667.     {
  668.     CalcWindowAreaDeviceLoopUserData *    deviceLoopDataPtr;
  669.     long                                windowAreaOnThisScreen;
  670.     Rect                                windowRectOnThisScreen;
  671.     
  672.     deviceLoopDataPtr = (CalcWindowAreaDeviceLoopUserData *) userData;
  673.  
  674.     SectRect(&deviceLoopDataPtr->fWindowBounds, &(**targetDevice).gdRect,&windowRectOnThisScreen);
  675.     OffsetRect(&windowRectOnThisScreen,-windowRectOnThisScreen.left,-windowRectOnThisScreen.top);
  676.     windowAreaOnThisScreen = windowRectOnThisScreen.right * windowRectOnThisScreen.bottom;
  677.  
  678.     if (windowAreaOnThisScreen > deviceLoopDataPtr->fLargestArea)
  679.         {
  680.         deviceLoopDataPtr->fLargestArea = windowAreaOnThisScreen;
  681.         deviceLoopDataPtr->fScreenWithLargestPartOfWindow = targetDevice;
  682.         }
  683.     }
  684.  
  685.  
  686. DeviceLoopDrawingUPP CallCalcWindowAreaOnDevice = NewDeviceLoopDrawingProc(&CalculateWindowAreaOnDevice);
  687.  
  688.  
  689. void
  690. FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect,GDHandle * theBestDevice)
  691.     {
  692.     RgnHandle                            copyOfWindowStrucRgn;
  693.     CalcWindowAreaDeviceLoopUserData    deviceLoopData;
  694.  
  695.     //    Use DeviceLoop to find out what GDevice contains the largest
  696.     //    portion of the supplied window.
  697.     //
  698.     //    NOTE:    Assumes thePort == the Window Manager Port because we using
  699.     //            the window strucRgn, not contRgn.
  700.  
  701.     deviceLoopData.fScreenWithLargestPartOfWindow = nil;
  702.     deviceLoopData.fLargestArea = -1;
  703.     deviceLoopData.fWindowBounds = (**(((WindowPeek) aWindow)->contRgn)).rgnBBox;
  704.     
  705.     copyOfWindowStrucRgn = NewRgn();
  706.     CopyRgn(((WindowPeek) aWindow)->strucRgn,copyOfWindowStrucRgn);
  707.  
  708.     DeviceLoop(copyOfWindowStrucRgn,CallCalcWindowAreaOnDevice,(long) &deviceLoopData,singleDevices);    
  709.  
  710.     DisposeRgn(copyOfWindowStrucRgn);
  711.     
  712.     *theBestDevice = deviceLoopData.fScreenWithLargestPartOfWindow;
  713.     *theBestScreenRect = (**(deviceLoopData.fScreenWithLargestPartOfWindow)).gdRect;
  714.  
  715.     //    Leave some space around the edges of the screen so window look good, AND
  716.     //    if the best device is the main screen, leave space for the Menubar
  717.     
  718.     InsetRect(theBestScreenRect,kScreenEdgeSlop,kScreenEdgeSlop);
  719.     if (GetMainDevice() == deviceLoopData.fScreenWithLargestPartOfWindow)
  720.         theBestScreenRect->top += GetMBarHeight();
  721.     }
  722.  
  723.  
  724. ///////////////////////////////////////////////////////////////////////////
  725. //
  726. //    Drag Manager callback routines which dispatch to a window’s method
  727. //
  728.  
  729. pascal OSErr    CallWindowDragTrackingHandler(DragTrackingMessage dragMessage,WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  730.     {
  731.     TWindow *wobj = GetWindowObject(theWindow);
  732.     
  733.     if (wobj)
  734.         return(wobj->HandleDrag(dragMessage,theDrag));
  735.     else
  736.         return(noErr);
  737.     }
  738.  
  739.     
  740. pascal OSErr    CallWindowDragReceiveHandler(WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  741.     {
  742.     TWindow *wobj = GetWindowObject(theWindow);
  743.     
  744.     if (wobj)
  745.         return(wobj->HandleDrop(theDrag));
  746.     else
  747.         return(noErr);
  748.     }
  749.